#include"time.h"
using namespace std;

bool operator>(Time a, Time b) {
	int Ax = a.hour * 3600 + a.minute * 60 + a.second;
	int Bx = b.hour * 3600 + b.minute * 60 + b.second;
	return Ax > Bx ? Ax : Bx;
}

bool operator<(Time a, Time b) {
	int Ax = a.hour * 3600 + a.minute * 60 + a.second;
	int Bx = b.hour * 3600 + b.minute * 60 + b.second;
	return Ax < Bx ? Ax : Bx;
}

bool operator==(Time a, Time b) {
	int Ax = a.hour * 3600 + a.minute * 60 + a.second;
	int Bx = b.hour * 3600 + b.minute * 60 + b.second;
	return Ax == Bx ? Ax : Bx;
}

bool operator >= (Time a, Time b) {
	int Ax = a.hour * 3600 + a.minute * 60 + a.second;
	int Bx = b.hour * 3600 + b.minute * 60 + b.second;
	return Ax >= Bx ? Ax : Bx;
}

bool operator<=(Time a, Time b) {
	int Ax = a.hour * 3600 + a.minute * 60 + a.second;
	int Bx = b.hour * 3600 + b.minute * 60 + b.second;
	return Ax <= Bx ? Ax : Bx;
}

bool isinPeriod(Time t, Period s) {    //判断时间是否在时段中
	if (t < s.begin) {
		return 0;
	}
	//到这里能保证比起始时间晚了
	if (s.nextlayer == 0) {
		if (t > s.end) {
			return 0;
		}
		else {
			return 1;
		}
	}
	else {
		int Tx = t.hour * 3600 + t.minute * 60 + t.second;
		int Sx = s.end.hour * 3600 + s.end.minute * 60 + s.end.second + s.nextlayer * 24 * 3600;
		return Tx <= Sx ? 1 : 0;
	}
}

bool isinDuratio(Time t, vector<Period>s) {      //判断时间是否在时段组合中
	if (s.size() == 0) { return 0; }
	for (int i = 0; i < s.size(); i++) {
		if (isinPeriod(t, s[i]) == 1) {
			return 1;
		}
	}
	return 0;
}

Time operator+ (Time a, Period b) {    //时间与时段的加法
	int h = a.hour + b.duration.hour+b.duration.layer;
	int m = a.minute + b.duration.minute;
	int s = a.second + b.duration.second;
	int day=0;
	while (s >= 60) {
		s -= 60;
		m++;
	}
	while (m >= 60) {
		m -= 60;
		h++;
	}
	while (h >= 24) {
		h -= 24;
		day++;
	}
	Time* p = new Time(h, m, s);
	p->Setlayer(day);
	return *p;
}

Time operator+ (Time a, Time b) {    //时间与时间的加法
	int h = a.hour + b.hour;
	int m = a.minute + b.minute;
	int s = a.second + b.second;
	int day=0;
	while (s >= 60) {
		s -= 60;
		m++;
	}
	while (m >= 60) {
		m -= 60;
		h++;
	}
	while (h >= 24) {
		h -= 24;
		day++;
	}
	Time* p = new Time(h, m, s);
	p->Setlayer(day);
	return *p;
}

Time operator- (Time a, Time b) {    //时间与时间的减法
	int h = a.hour - b.hour;
	int m = a.minute - b.minute;
	int s = a.second - b.second;
	while (s<0) {
		s += 60;
		m--;
	}
	while (m < 0) {
		m += 60;
		h--;
	}
	Time* p = new Time(h, m, s); 
	return *p;
}

Time operator% (Time a, Time b) {   //取余运算，公交导航需要用到
	int ax = a.hour * 3600 + a.minute * 60 + a.second;
	int bx = b.hour * 3600 + b.minute * 60 + b.second;
	int cx = ax % bx;
	int h = cx / 3600;
	cx -= h * 3600;
	int m = cx / 60;
	cx -= m * 60;
	int s = cx;
	Time* p = new Time(h, m, s);
	return *p;
}